package com.github.caijinglong.handler

import com.github.caijinglong.SaveDataHandler
import com.github.caijinglong.SavedDataType
import com.github.caijinglong.ext.println
import com.github.caijinglong.getGetter
import org.json.JSONArray
import org.json.JSONObject

class CardHandler : SaveDataHandler {
    override var jsonObject: JSONObject = JSONObject()

    val key = "cards"

    fun upgrade() {
        val cards = jsonObject.getJSONArray(key)
        val list = ArrayList<JSONObject>()
        cards.forEach {
            if (it is JSONObject) {
                it.put("upgrades", 1)
                list.add(it)
            }
        }
        jsonObject.put("cards", JSONArray(list))
    }

    fun removeDebuffCard() {
        val cards = jsonObject.getJSONArray(key)
        val c = cards.filter {
            if (it is JSONObject) {
                if (DEBUFF_CARD.contains(it.getString("id")))
                    return@filter false
            }
            true
        }
        jsonObject.put(key, c)
    }

    fun showAllCard() {
        val cards = jsonObject.getJSONArray(key)
        cards.println()
    }

    companion object {
        val DEBUFF_CARD = arrayListOf("Pain", "Clumsy", "Necronomicurse", "Normality", "Parasite")

        @JvmStatic
        fun main(args: Array<String>) {
            CardHandler().apply {
                bind(SavedDataType.ZS.getGetter())
                upgrade()
                save(SavedDataType.ZS.srcPath)
            }
        }
    }
}